package com.meritdata.cloud.data.service.development.controller;

import com.meritdata.cloud.data.service.development.model.DataApiCategory;
import com.meritdata.cloud.data.service.development.service.DataApiCategoryService;
import com.meritdata.cloud.resultmodel.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@RequestMapping("/apicategory")
@Controller
public class DataApiCategoryController {

    @Autowired
    private DataApiCategoryService service;

    @RequestMapping("/tree")
    @ResponseBody
    public ResultBody tree() {
        ResultBody r = new ResultBody();
        r.setData(service.tree());
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/create")
    @ResponseBody
    public ResultBody create(@RequestParam String name) {
        ResultBody r = new ResultBody();
        DataApiCategory d = new DataApiCategory();
        d.setName(name);
        d.setUpdateTime(new Date());
        d.setCreateTime(new Date());
        service.save(d);
        r.setData(d);
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResultBody update(@RequestParam String name, @RequestParam String id) {
        ResultBody r = new ResultBody();
        r.setData(service.udpate(id,name,new Date()));
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/del")
    @ResponseBody
    public ResultBody del(@RequestParam String id) {
        ResultBody r = new ResultBody();
        r.setData(service.delete(id));
        r.setSuccess(true);
        return r;
    }
}
